home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0041_Counting Lines.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  1KB  |  45 lines

  1. {
  2. >The subroutine opened a text file (in this case the Telix.USE file) in
  3. >binary mode, and then searched through the file for the CR/LF pair and
  4. >then incremented a counter.  At the end I knew the number of lines in
  5. >the text file.  I suppose in Pascal I could open the file do a while
  6. >loop and count the lines -- but that would require me to read every
  7. >single line where the basic subroutine did all the searching without
  8. >having to read the file line by line.
  9.  
  10. >I guess what I'm asking is how is a fast way to determine the number of
  11. >lines in a text file using Pascal.
  12.  
  13. FWIW, This routine takes a little over 6 seconds on a 330K TELIX.USE on a
  14. 386/33
  15. }
  16.  
  17. program countlines;
  18.  
  19. var
  20.    usefile : file;
  21.    buffer :  array[0..8191] of byte;
  22.    counter, numw, numr : word;
  23.    size, numlines : longint;
  24.  
  25.  
  26. begin
  27.    numlines := 0;
  28.    counter := 0;
  29.    fillchar(buffer, sizeof(buffer), #0);
  30.    assign(usefile,'TELIX.USE');
  31.    reset(usefile,1);
  32.    size := filesize(usefile);
  33.    repeat
  34.       blockread(usefile,buffer,sizeof(buffer),numr);
  35.       for counter := 0 to 8191 do
  36.          if buffer[counter] = ord(13)
  37.             then begin
  38.                     inc(numlines);
  39.                     write(round((filepos(usefile)/size)*100),'%',chr(13));
  40.                  end;
  41.    until numr = 0;
  42.    close(usefile);
  43.    writeln('Your TELIX.USE has ',numlines,' lines.');
  44. end.
  45.